package cn.caojun.auth.controller;

import cn.caojun.auth.annotation.caojunPermission;
import cn.caojun.auth.domain.Role;
import cn.caojun.auth.dto.RoleMenuDTO;
import cn.caojun.auth.dto.RolePermissionDTO;
import cn.caojun.auth.query.RoleQuery;
import cn.caojun.auth.service.IRoleService;
import cn.caojun.basic.utils.AjaxResult;
import cn.caojun.basic.utils.PageList;
import cn.caojun.org.domain.Department;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/role")
@caojunPermission(name = "用户管理",desc = "用户权限 ")
public class RoleController {

    @Autowired
    public IRoleService roleService;

    @PutMapping
    @ApiOperation(value="用户新增或修改")
    @caojunPermission(name = "用户新增或修改",desc = "用户新增或修改管理权限 ")
    public AjaxResult addOrUpdata(@RequestBody Role role){
        try{
            if (Objects.isNull(role.getId())){
                roleService.add(role);
            }else {
                roleService.update(role);
            }
            return AjaxResult.me();
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage("数据修改失败"+e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value="根据id删除用户")
    @caojunPermission(name = "根据id删除用户",desc = "根据id删除用户 ")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try{
            roleService.removeById(id);
            return AjaxResult.me();
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage("数据删除失败"+e.getMessage());
        }
    }

    @PatchMapping
    @Transactional
    @ApiOperation(value="删除用户")
    @caojunPermission(name = "批量删除用户",desc = "批量删除用户 ")
    public AjaxResult patchDelete(@RequestBody List<Long> list){
        try{
            roleService.batchRemove(list);
            return AjaxResult.me();
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败"+e.getMessage());
        }
    }

    @GetMapping
    public AjaxResult getAll(){
        try{
            List<Role> roleList=roleService.getAll();
            return AjaxResult.me().setObject(roleList);
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage("获取全部用户失败"+e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value="根据id查找用户")
    @caojunPermission(name = "根据id查找用户",desc = "根据id查找用户 ")
    public AjaxResult getById(@PathVariable("id") Long id){
        try{
            Role role=roleService.getById(id);
            return AjaxResult.me().setObject(role);
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage("获取用户失败"+e.getMessage());
        }
    }
    @GetMapping("/permissionSns/{roleId}")
    public AjaxResult getAllPermissionById(@PathVariable("roleId") Long roleId){
        try{
            List<String> rolePermissionList=roleService.getPermissionById(roleId);
            return AjaxResult.me().setObject(rolePermissionList);
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @GetMapping("/menuId/{roleId}")
    public AjaxResult getAllMenuById(@PathVariable("roleId") Long roleId){
        try{
            List<String> roleMenuList=roleService.getMenuById(roleId);
            return AjaxResult.me().setObject(roleMenuList);
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @PostMapping
    @ApiOperation(value="查找当前页面用户")
    @caojunPermission(name = "查找当前页面用户",desc = "查找当前页面用户 ")
    public AjaxResult getRoleList(@RequestBody RoleQuery roleQuery){
        try{
            PageList<Role> roles=roleService.getLimteList(roleQuery);
            return AjaxResult.me().setObject(roles);
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage("根据页表获取用户失败"+e.getMessage());
        }
    }

    @PutMapping("/permission")
    public AjaxResult setPermission(@RequestBody RolePermissionDTO rolePermissionDTO){
        try{
            roleService.setRolePermission(rolePermissionDTO);
            return AjaxResult.me();
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage("权限修改失败"+e.getMessage());
        }
    }
    @PutMapping("/menu")
    public AjaxResult setMenu(@RequestBody RoleMenuDTO roleMenuDTO){
        try{
            roleService.setRoleMenu(roleMenuDTO);
            return AjaxResult.me();
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage("权限修改失败"+e.getMessage());
        }
    }
}
